
export var isLogin = () => {
  return myLocalStorage.get('sessionid');
}


export var mySessionstorage = {
  set(key:string, val:any) {
    try {
      sessionStorage.setItem(key, JSON.stringify(val)); //存入缓存值  
    } catch (e) {console.log(e)}
  },
  get(key:string) {
    try {
      if (!sessionStorage) {
        return false;
      }
      const cacheVal:any = sessionStorage.getItem(key);
      const result = JSON.parse(cacheVal);
      if (!result) {
        return null;
      } //缓存不存在  
      return result;
    } catch (e) {
      return null;
    }
  },
  /**移除缓存*/
  remove: function (key) {
    if (!sessionStorage) {
      return false;
    }
    sessionStorage.removeItem(key);
  },

}

//数据存储
export var myLocalStorage = {
  set: function (key, stringVal, time) {
    try {
      if (!localStorage) {
        return false;
      }
      if (!time || isNaN(time)) {
        time = 60000000000000;
      }
      var cacheExpireDate = (new Date() - 1) + time * 1000;
      var cacheVal = {
        val: stringVal,
        exp: cacheExpireDate
      };
      localStorage.setItem(key, JSON.stringify(cacheVal)); //存入缓存值  
    } catch (e) {console.log(e)}
  },
  /**获取缓存*/
  get: function (key) {
    try {
      if (!localStorage) {
        return false;
      }
      var cacheVal = localStorage.getItem(key);
      var result = JSON.parse(cacheVal);
      var now = new Date() - 1;
      if (!result) {
        return null;
      } //缓存不存在  
      if (now > result.exp) { //缓存过期  
        this.remove(key);
        return "";
      }
      return result.val;
    } catch (e) {
      this.remove(key);
      return null;
    }
  },
  /**移除缓存，一般情况不手动调用，缓存过期自动调用*/
  remove: function (key) {
    if (!localStorage) {
      return false;
    }
    localStorage.removeItem(key);
  },
  /**清空所有缓存*/
  clear: function () {
    if (!localStorage) {
      return false;
    }
    localStorage.clear();
  }
}

export var FormatTime = (date) => {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;

}


export default {
  mySessionstorage,
  myLocalStorage,
  isLogin,
  FormatTime,
}